import 'package:flutter/material.dart';
import 'package:sky_ui/sky_ui.dart';

class InfiniteScrollDemo1 extends StatefulWidget {
  const InfiniteScrollDemo1({super.key});

  @override
  State<InfiniteScrollDemo1> createState() => _InfiniteScrollDemo1State();
}

class _InfiniteScrollDemo1State extends State<InfiniteScrollDemo1> {
  late List<int> listData = [];
  late bool loadFinish = false;
  late bool loading = false;
  @override
  void initState() {
    super.initState();
    setState(() {
      listData = List.generate(10000, (index) => index);
    });
  }

  void loadMore() async {
    if (loading) {
      return;
    }
    setState(() {
      loading = true;
    });
    await Future.delayed(const Duration(seconds: 2), () {
      listData = [
        ...listData,
        ...List.generate(10, (index) => index + listData.length),
      ];
      if (mounted) {
        setState(() {
          loadFinish = listData.length > 50;
          loading = false;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          height: 400,
          child: SkyInfiniteScroll(
            loadFinish: loadFinish,
            loading: loading,
            loadMore: loadMore,
            data: listData,
            itemBuilder: (context, index) {
              return Container(
                height: 40,
                alignment: Alignment.center,
                margin: const EdgeInsets.symmetric(vertical: 10),
                color: const Color(0xFF7dbcfc),
                child: Text("选项$index"),
              );
            },
          ),
        ),
      ],
    );
  }
}
